//
//  PSGameSettingTests.m
//  PrefCore
//
//  Created by Sergey on 1/25/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "PSGameSettings.h"

@interface PSGameSettingTests : XCTestCase {
    NSUserDefaults *ud;
    PSGameSettings *gs;
}

@end

@implementation PSGameSettingTests

-(void)setUp {
    ud = [NSUserDefaults standardUserDefaults];
    [PSGameSettings clearUserDefaults:ud];
    [ud synchronize];
    gs = [PSGameSettings settingsFromUserDefaults:ud];
}

-(void)testInitialSettings {
    XCTAssertEqual(gs.poolSize, 20);
    XCTAssertEqual(gs.gameFlavor, kGameFlavorSochi);
    XCTAssertEqual(gs.passingType, kPassingTypeSliding);
    XCTAssertEqual(gs.passingProgression,  kPassingProgression_123123);
    XCTAssertEqual(gs.passingTrickCost, kPassingTrickCost_1);
    XCTAssertEqual(gs.passingExit, kPassingExit_6777);
    XCTAssertEqual(gs.misereCatchMode, kMisereCacthModeDefault);
    XCTAssertEqual(gs.trickingTen, kTricking10Checked);
    XCTAssertEqual(gs.whistType, kWhistGreedy);
    XCTAssertTrue(gs.mandatoryWhistOnSixSpades);
    XCTAssertEqual(gs.suitOrder, kSuitOrderByColor);
    XCTAssertEqual(gs.rankOrder, kRankOrderAscending);
    XCTAssertFalse(gs.spaceBetweenSuits);
    XCTAssertEqual(gs.bidConfirmation, kConfirmationAlways);
    XCTAssertEqual(gs.contractConfirmation, kConfirmationAlways);
    XCTAssertTrue(gs.roundEndTapWhenNotPlaying);
    XCTAssertTrue(gs.roundEndTapWhenLastMoverAi);
    XCTAssertFalse(gs.roundEndTapWhenLastMoverHuman);
    XCTAssertFalse(gs.automateObviousMoves);
    XCTAssertFalse(gs.skipGameOnPass);
    XCTAssertFalse(gs.isPassingInterruptedByFailedGame);
    XCTAssertFalse(gs.isPassingInterruptedByNonWhistedGame);
    XCTAssertFalse([gs isPassingInterruptedByMisereWithTricks:0]);
    for (PSTrickCount tricks = 1 ; tricks <= 10 ; tricks++) {
        XCTAssertFalse([gs isPassingInterruptedByMisereWithTricks:tricks]);
    }
    XCTAssertEqual(kGameLevel0, [gs levelForPlayerId:1]);
    XCTAssertEqual(kGameLevel0, [gs levelForPlayerId:2]);
}

-(void)testSaveSettings {
    // Change the settings from their default values
    gs.poolSize = 30;
    gs.gameFlavor = kGameFlavorRostov;
    gs.passingType = kPassingTypeClassic;
    gs.passingProgression = kPassingProgression_1248;
    gs.passingTrickCost = kPassingTrickCost_2;
    gs.passingExit = kPassingExit_678678;
    gs.misereCatchMode = kMisereCacthModeHuman;
    gs.trickingTen = kTricking10Whisted;
    gs.whistType = kWhistGentleman;
    gs.mandatoryWhistOnSixSpades = NO;
    gs.suitOrder = kSuitOrderByRank;
    gs.rankOrder = kRankOrderDescending;
    gs.spaceBetweenSuits = YES;
    gs.bidConfirmation = kConfirmationIntelligent;
    gs.contractConfirmation = kConfirmationNever;
    gs.roundEndTapWhenNotPlaying = NO;
    gs.roundEndTapWhenLastMoverAi = NO;
    gs.roundEndTapWhenLastMoverHuman = YES;
    gs.automateObviousMoves = YES;
    gs.skipGameOnPass = YES;
    [gs setLevel:kGameLevel1 forPlayerId:1];
    [gs setLevel:kGameLevel2 forPlayerId:2];
    // Verify the new settings in the immediate object
    XCTAssertEqual(gs.poolSize, 30);
    XCTAssertEqual(gs.gameFlavor, kGameFlavorRostov);
    XCTAssertEqual(gs.passingType, kPassingTypeClassic);
    XCTAssertEqual(gs.passingProgression, kPassingProgression_1248);
    XCTAssertEqual(gs.passingTrickCost, kPassingTrickCost_2);
    XCTAssertEqual(gs.passingExit, kPassingExit_678678);
    XCTAssertEqual(gs.misereCatchMode, kMisereCacthModeHuman);
    XCTAssertEqual(gs.trickingTen, kTricking10Whisted);
    XCTAssertEqual(gs.whistType, kWhistGentleman);
    XCTAssertFalse(gs.mandatoryWhistOnSixSpades);
    XCTAssertEqual(gs.suitOrder, kSuitOrderByRank);
    XCTAssertEqual(gs.rankOrder, kRankOrderDescending);
    XCTAssertTrue(gs.spaceBetweenSuits);
    XCTAssertEqual(gs.bidConfirmation, kConfirmationIntelligent);
    XCTAssertEqual(gs.contractConfirmation, kConfirmationNever);
    XCTAssertFalse(gs.roundEndTapWhenNotPlaying);
    XCTAssertFalse(gs.roundEndTapWhenLastMoverAi);
    XCTAssertTrue(gs.roundEndTapWhenLastMoverHuman);
    XCTAssertTrue(gs.automateObviousMoves);
    XCTAssertTrue(gs.skipGameOnPass);
    XCTAssertEqual(kGameLevel1, [gs levelForPlayerId:1]);
    XCTAssertEqual(kGameLevel2, [gs levelForPlayerId:2]);
    // Save and synchronize
    [gs saveToUserDefaults:ud];
    [ud synchronize];
    // Make sure that synchronizing does not invalidate the settings
    XCTAssertEqual(gs.poolSize, 30);
    XCTAssertEqual(gs.gameFlavor, kGameFlavorRostov);
    XCTAssertEqual(gs.passingType, kPassingTypeClassic);
    XCTAssertEqual(gs.passingProgression, kPassingProgression_1248);
    XCTAssertEqual(gs.passingTrickCost, kPassingTrickCost_2);
    XCTAssertEqual(gs.passingExit, kPassingExit_678678);
    XCTAssertEqual(gs.misereCatchMode, kMisereCacthModeHuman);
    XCTAssertEqual(gs.trickingTen, kTricking10Whisted);
    XCTAssertEqual(gs.whistType, kWhistGentleman);
    XCTAssertFalse(gs.mandatoryWhistOnSixSpades);
    XCTAssertEqual(gs.suitOrder, kSuitOrderByRank);
    XCTAssertEqual(gs.rankOrder, kRankOrderDescending);
    XCTAssertTrue(gs.spaceBetweenSuits);
    XCTAssertEqual(gs.bidConfirmation, kConfirmationIntelligent);
    XCTAssertEqual(gs.contractConfirmation, kConfirmationNever);
    XCTAssertFalse(gs.roundEndTapWhenNotPlaying);
    XCTAssertFalse(gs.roundEndTapWhenLastMoverAi);
    XCTAssertTrue(gs.roundEndTapWhenLastMoverHuman);
    XCTAssertTrue(gs.automateObviousMoves);
    XCTAssertTrue(gs.skipGameOnPass);
    XCTAssertEqual(kGameLevel1, [gs levelForPlayerId:1]);
    XCTAssertEqual(kGameLevel2, [gs levelForPlayerId:2]);
    // Read the settings back from a different instance of user defaults
    NSUserDefaults *ud2 = [NSUserDefaults standardUserDefaults];
    PSGameSettings *gs2 = [PSGameSettings settingsFromUserDefaults:ud2];
    // Verify that the settings in the new object match the updated ones
    XCTAssertEqual(gs2.poolSize, 30);
    XCTAssertEqual(gs2.gameFlavor, kGameFlavorRostov);
    XCTAssertEqual(gs2.passingType, kPassingTypeClassic);
    XCTAssertEqual(gs2.passingProgression, kPassingProgression_1248);
    XCTAssertEqual(gs2.passingTrickCost, kPassingTrickCost_2);
    XCTAssertEqual(gs2.passingExit, kPassingExit_678678);
    XCTAssertEqual(gs2.misereCatchMode, kMisereCacthModeHuman);
    XCTAssertEqual(gs2.trickingTen, kTricking10Whisted);
    XCTAssertEqual(gs2.whistType, kWhistGentleman);
    XCTAssertFalse(gs2.mandatoryWhistOnSixSpades);
    XCTAssertEqual(gs2.suitOrder, kSuitOrderByRank);
    XCTAssertEqual(gs2.rankOrder, kRankOrderDescending);
    XCTAssertTrue(gs2.spaceBetweenSuits);
    XCTAssertEqual(gs2.bidConfirmation, kConfirmationIntelligent);
    XCTAssertEqual(gs2.contractConfirmation, kConfirmationNever);
    XCTAssertFalse(gs2.roundEndTapWhenNotPlaying);
    XCTAssertFalse(gs2.roundEndTapWhenLastMoverAi);
    XCTAssertTrue(gs2.roundEndTapWhenLastMoverHuman);
    XCTAssertTrue(gs2.automateObviousMoves);
    XCTAssertTrue(gs2.skipGameOnPass);
    XCTAssertEqual(kGameLevel1, [gs2 levelForPlayerId:1]);
    XCTAssertEqual(kGameLevel2, [gs2 levelForPlayerId:2]);
}

@end
